<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>005-地理定位</title>
		<!--引入百度 API，"ak=" 后面一串码是密钥，最好自己申请-->
		<script
			type="text/javascript"
			src="https://api.map.baidu.com/api?v=2.0&ak=7a6QKaIilZftIMmKGAFLG7QT1GLfIncg"
		></script>
	</head>
	<body>
		<input type="button" onclick="getLocation()" value="确认" />
		<div id="position"></div>

		<script>
			let x = document.getElementById('position')
			function getLocation() {
				// 创建百度地理位置实例，代替 navigator.geolocation
				let geolocation = new BMap.Geolocation()
				geolocation.getCurrentPosition(function (e) {
					if (this.getStatus() == BMAP_STATUS_SUCCESS) {
						// 百度 geolocation 的经纬度属性不同，此处是 point.lat 而不是 coords.latitude
						x.innerHTML = '纬度：' + e.point.lat + '<br/>经度：' + e.point.lng
					} else {
						x.innerHTML = 'failed' + this.getStatus()
					}
				})
			}
		</script>
	</body>
</html>
